package com.wuql.create;

import com.wuql.create.bean.ClassColumn;
import com.wuql.create.service.ColumnService;
import com.wuql.create.util.FreeMarkerUtil;
import com.wuql.create.util.UnderlineToCamelUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成工具
 * @author : wuQingLiang
 * @date : 2024/3/14
 **/
public class CreateCode {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        ColumnService columnService =(ColumnService) applicationContext.getBean("columnService");
        /********************以下参数执行时需要根据实际项目修改**************************/
        String tableSchema="db_indstd_core";   //mysql数据库名称
        String tableName="indstd_data_asset_field"; //数据表名称
        String beanName="DataAssetField";  //要生成的实体类名称
        String moduleName="数据资产数据表字段";  //要生成的模块名称
        String rootPath="E:/workspace/"; //生成的代码保存根目录
        String packageStr="com.lnsoft.data"; //生成的代码保存包路径
        /********************以上参数执行时需要根据实际项目修改**************************/

        /********************以下代码一般不需要修改**************************/
        String basePath=rootPath+"/src/main/java/"+packageStr.replace(".","/");
        String beanPath=basePath+"/domain/"+beanName+".java";
        String queryPath=basePath+"/query/"+beanName+"Query.java";
        String mapperPath=basePath+"/mapper/"+beanName+"Mapper.java";
        String servicePath=basePath+"/service/"+beanName+"Service.java";
        String controllerPath=basePath+"/controller/"+beanName+"Controller.java";
        String serviceImplPath=basePath+"/service/impl/"+beanName+"ServiceImpl.java";
        String xmlPath=rootPath+"/src/main/resources/mapper/"+beanName+"Mapper.xml";
        List<ClassColumn> columnList=columnService.queryColumnList(tableSchema,tableName);//数据表所有字段
        ClassColumn keyColumn=columnService.getTableKey(tableSchema,tableName);//数据表主键
        String beanParam= UnderlineToCamelUtils.firstCharToLower(beanName);//参数名称
        Map<String,Object> data = new HashMap<>();
        data.put("packageStr", packageStr);
        data.put("beanName", beanName);
        data.put("beanParam", beanParam);
        data.put("tableName", tableName);
        data.put("columnList", columnList);
        data.put("keyColumn", keyColumn);
        data.put("moduleName", moduleName);

        /********************以下ftl模版内容可根据实际项目修改**************************/
        FreeMarkerUtil.createFile(data,"bean.ftl",beanPath);//创建bean
        FreeMarkerUtil.createFile(data,"query.ftl",queryPath);//创建bean
        FreeMarkerUtil.createFile(data,"xml.ftl",xmlPath);//创建mapper.xml
        FreeMarkerUtil.createFile(data,"mapper.ftl",mapperPath);//创建mapper.java
        FreeMarkerUtil.createFile(data,"service.ftl",servicePath);//创建service.java
        FreeMarkerUtil.createFile(data,"serviceImpl.ftl",serviceImplPath);//创建serviceImpl.java
        FreeMarkerUtil.createFile(data,"controller.ftl",controllerPath);//创建controller.java
        System.out.println("--------代码生成成功，请打开文件路径"+rootPath+"查看生成的代码文件---------");

    }
}
